package com.yao.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.sys.entity.dto.AccountDto;
import com.yao.sys.entity.dto.MenuDto;
import com.yao.sys.service.AccountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 账号表 Controller
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-04-09
 */
@Api(tags = "账号管理")
@RestController
@RequestMapping(value = "/account")
@Slf4j
public class AccountController {

    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "新增账号", notes = "新增账号")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Void> add(@RequestBody AccountDto model) {

        ValidateHelper.validateNull(model, new String[] {"accountName", "password", "name", "phone", "idCard"});

        accountService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改账号", notes = "修改账号")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Void> update(@RequestBody AccountDto model) {
        ValidateHelper.validateNull(model, new String[] {"id", "accountName", "password", "name", "phone", "idCard"});

        accountService.modifyById(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "删除账号", notes = "逻辑删除账号")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteById(@PathVariable("id") Integer id) {
        ValidateHelper.validateId(id);

        accountService.removeById(id);

        return ApiResponse.success();
    }

    @ApiOperation(value = "获取账号", notes = "获取账号")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ApiResponse<MenuDto> get(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        AccountDto account = accountService.getById(id);

        account.setIdCard("452122199003140050");

        return ApiResponse.success(account);
    }

    @ApiOperation(value = "账号列表(分页)", notes = "账号列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<IPage<AccountDto>> list(@RequestBody PageQueryBean<AccountDto> queryBean) {

        return ApiResponse
            .success(accountService.page(new Page<>(queryBean.getCurrent(), queryBean.getSize()), queryBean.getData()));
    }

}